<template>
    <div>
        <h2>{{story.title}}</h2>
        <p>{{story.what}}</p>
        <p><button @click="logout_story">离开</button></p>
        <p><button @click="putUser" v-show="user.level==0">升级VIP</button></p>
    </div>
</template>

<script>
    export default {
        name: "show_story",
        data() {
            return {
                story: '',
                user: JSON.parse(sessionStorage.getItem('user')),
                story_id: this.$route.query.story_id,
            }
        },
        created() {
            this.$axios({
                url: 'story/story/',
                params: {
                    story_id: this.story_id
                }
            }).then(resp=>{
                this.story = resp.data[0];
                this.postRecord()
            })
        },
        methods: {
            postRecord(){
                this.$axios({
                    url: 'story/record/',
                    method: 'post',
                    data: {
                        user_id: this.user.id,
                        story_id: this.story_id
                    }
                }).then(resp=>{
                    alert(resp.data.msg)
                })
            },
            logout_story(){
                this.$axios({
                    url: 'story/record/',
                    method: 'put',
                    data: {
                        user_id: this.user.id,
                        story_id: this.story_id
                    }
                }).then(resp=>{
                    alert(resp.data.msg)
                    if (resp.data.code==200){
                        this.$router.push('/index_story')
                    }
                })
            },
            putUser(){
                this.$axios({
                    url: 'story/user/',
                    method: 'put',
                    data: {
                        user_id: this.user.id,
                    }
                }).then(resp=>{
                    alert(resp.data.msg)
                    if (resp.data.code==200){
                        sessionStorage.setItem('user', JSON.stringify(resp.data.user));
                        // this.$router.go(0)
                    }
                })
            }
        },
        computed: {},
        filters: {},
        watch: {},
    }
</script>

<style scoped>

</style>
